const a: nameType = { name: "234" };
console.log(a);

import Koa from "koa";

import { indexRouter, user, file, test, mysqlControl } from "./router";

import Mongoose from "mongoose";

import GriFiled from "gridfs-stream";

import KoaBody from "koa-body";

import koaStatic from "koa-static";

import { MONGOOSE_URL, mysqlconfig } from "@/config/db";

import mysql from "mysql";

import errorMiddleware from "@/middleware/handleError";

import { accessLogger } from "@/middleware/log";

global.mysqldb = mysql.createConnection(mysqlconfig);

global.mysqlpool = mysql.createPool(mysqlconfig)

global.mysqldb.connect((err: any) => {
  console.log(`连接mysql${err ? "失败" : "成功"}`);
});

// mongodb文件系统
const conn = Mongoose.createConnection(MONGOOSE_URL);
conn.on("open", () => {
  console.log("连接文件数据库成功");
  global.gfs = GriFiled(conn.db, Mongoose.mongo); // 挂载全局
});
conn.on("error", () => {
  console.log("连接文件数据库失败");
});

// mongodb连接数据库
Mongoose.set("debug", true);
Mongoose.connect(MONGOOSE_URL);
Mongoose.connection.on("open", () => {
  console.log("连接mongoDb数据库成功");
});
Mongoose.connection.on("error", () => {
  console.log("连接mongoDb数据库失败");
});

const app = new Koa();

app.use(errorMiddleware);
app.use(accessLogger());

app.use(
  KoaBody({
    multipart: true,
    formidable: {
      maxFileSize: 20 * 1024 * 1024, // 设置上传文件大小最大限制，默认2M
    },
  })
);

app.use(koaStatic("./public"));

app.use(indexRouter.routes());
app.use(user.routes());
app.use(file.routes());
app.use(test.routes());
app.use(mysqlControl.routes());

app.listen(1122);